{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if后面的条件表达式可以通过and，or，not实现多重条件判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "hi = 6\n",
    "if hi > 2:\n",
    "    if hi > 7:\n",
    "        print('好棒!好棒!')\n",
    "else:\n",
    "    print('切~')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "切~\n"
     ]
    }
   ],
   "source": [
    "hi = 6\n",
    "if hi > 2:\n",
    "    if hi > 7:\n",
    "        print('好棒!好棒!')\n",
    "    else:\n",
    "        print('切~')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入成绩:50\n",
      "D\n"
     ]
    }
   ],
   "source": [
    "temp = input('请输入成绩:')\n",
    "source = int(temp)\n",
    "if 100 >= source >= 90:\n",
    "    print('A')\n",
    "elif 90 > source >= 80:\n",
    "    print('B')\n",
    "elif 80 > source >= 60:\n",
    "    print('C')\n",
    "elif 60 > source >= 0:\n",
    "    print('D')\n",
    "else:\n",
    "    print('输入错误！')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert 当这个关键词后边的条件为 False 时，程序自动崩溃并抛出 AssertionError 的异常。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-e0ff2a9a0258>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mmy_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmy_list\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAssertionError\u001b[0m: "
     ]
    }
   ],
   "source": [
    "my_list = ['lsgogroup']\n",
    "my_list.pop(0)\n",
    "len(my_list)\n",
    "assert len(my_list) > 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "while 循环的代码块会一直循环执行，直到while后面的值为假\n",
    "while后面可以写入 str、list 或任何序列，长度非零则视为真值，执行循环体；只根据长度是否为0判断是否执行循环体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "abcd\n",
      "bcd\n",
      "cd\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "string = 'abcd'\n",
    "while string:\n",
    "    print(string)\n",
    "    string = string[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 is less than 5\n",
      "1 is less than 5\n",
      "2 is less than 5\n",
      "3 is less than 5\n",
      "4 is less than 5\n",
      "5 is not less than 5\n"
     ]
    }
   ],
   "source": [
    "#while else的执行逻辑 只有当跳出while循环时 才会执行else的语句 与if else逻辑不同\n",
    "count = 0\n",
    "while count < 5:\n",
    "    print(\"%d is less than 5\" % count)\n",
    "    count = count + 1\n",
    "else:\n",
    "    print(\"%d is not less than 5\" % count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for可以遍历任何有序序列，如 str、list、tuple等，也可以遍历任何可迭代对象，如dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a:1  b:2  c:3  d:4  "
     ]
    }
   ],
   "source": [
    "#注意遍历字典的形式\n",
    "dic = {'a': 1, 'b': 2, 'c': 3, 'd': 4}\n",
    "for key,values in dic.items():\n",
    "    print(key,values,sep=\":\",end=\"  \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a b c d "
     ]
    }
   ],
   "source": [
    "#也可以只遍历键或值\n",
    "dic = {'a': 1, 'b': 2, 'c': 3, 'd': 4}\n",
    "for key in dic.keys():\n",
    "    print(key, end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for else 同 while else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "range函数包含头但不包括尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "enumerate()函数，作用在一个序列、迭代器或其他支持迭代对象，主要作用是编号？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]\n"
     ]
    }
   ],
   "source": [
    "seasons = ['Spring', 'Summer', 'Fall', 'Winter']\n",
    "lst = list(enumerate(seasons))\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]\n"
     ]
    }
   ],
   "source": [
    "lst = list(enumerate(seasons,start=1))\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 I love Python\n",
      "1 I love R\n",
      "2 I love Matlab\n",
      "3 I love C++\n"
     ]
    }
   ],
   "source": [
    "languages = ['Python', 'R', 'Matlab', 'C++']\n",
    "for i, language in enumerate(languages):\n",
    "    print(i, 'I love', language)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "1\n",
      "4\n",
      "3\n",
      "6\n",
      "5\n",
      "8\n",
      "7\n",
      "10\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "#continue 终止本轮循环并开始下一轮循环。break是直接跳出循环语句\n",
    "for i in range(10):\n",
    "    if i % 2 != 0:\n",
    "        print(i)\n",
    "        continue\n",
    "    i += 2\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 4, 9, 16, 25, 36, 49, 64, 81]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表推导式 可以使函数表达更加简短 \n",
    "[i ** 2 for i in range(1, 10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i for i in range(100) if (i % 2) != 0 and (i % 3) == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n"
     ]
    }
   ],
   "source": [
    "#元组推导式\n",
    "a = (x for x in range(10))\n",
    "print(tuple(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "#集合推导式 重复元素只保留一个\n",
    "c = {i for i in [1, 2, 3, 4, 5, 5, 6, 4, 3, 2, 1]}\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "passwdList = ['123', '345', '890']\n",
    "valid = False\n",
    "count = 3\n",
    "while count > 0:\n",
    "    password = input('enter password:')\n",
    "    for item in passwdList:\n",
    "        if password == item:\n",
    "            valid = True\n",
    "            break\n",
    "\n",
    "    if not valid:\n",
    "        print('invalid input')\n",
    "        count -= 1\n",
    "        continue\n",
    "    else:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#练习题一\n",
    "a = []\n",
    "for i in range(1500,2701):\n",
    "    if i%35 == 0:\n",
    "        a.append(i)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
